package io.renren.modules.app.controller;


import io.renren.common.exception.MedicalException;
import io.renren.common.result.Result;
import io.renren.common.result.ResultCodeEnum;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.medical.entity.VisitRecordEntity;
import io.renren.modules.medical.entity.VisitRecordStepEntity;
import io.renren.modules.medical.service.VisitRecordService;
import io.renren.modules.medical.service.VisitRecordStepService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * @author Luo Sheng
 * @school YiBin university
 * @create 2023-04-24 11:07
 */
@RestController
@RequestMapping("/app/visit")
@RequiredArgsConstructor
public class VisitRecordApi {

    private final VisitRecordService visitRecordService;

    private final VisitRecordStepService visitRecordStepService;

    /**
     * 获取到随访完成与没完成的集合
     * @return
     */
    @GetMapping("/record")
    public Result<Map<String, List<VisitRecordEntity>>> getRecord(){
        Map<String, List<VisitRecordEntity>> resultMap = visitRecordService.getRecord();
        return Result.success(resultMap);
    }

    /**
     * 根据id获取某个阶段详情集合
     * @param id
     * @return
     */
    @GetMapping("/stepList/{id}")
    public Result<List<VisitRecordStepEntity>> getStepList(@PathVariable Long id){
        List<VisitRecordStepEntity> stepList = visitRecordStepService.getStepList(id);
        return Result.success(stepList);
    }


    /**
     * 获取阶段信息详情
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public Result<VisitRecordStepEntity> getDetail(@PathVariable("id") Long id){
        VisitRecordStepEntity stepEntity = visitRecordService.getDetail(id);
        return Result.success(stepEntity);

    }

    /**
     * 提交随访
     * @param stepEntity
     * @return
     */
    @PostMapping("/confirm")
    public Result<String> confirm(@Validated(UpdateGroup.class) @RequestBody VisitRecordStepEntity stepEntity){
        ValidatorUtils.validateEntity(stepEntity, UpdateGroup.class);
        if (stepEntity.getVisitRecordId() == null){
            throw new MedicalException(ResultCodeEnum.MODEL_INVALID_ERROR.getCode(), "提交时随访id不能为空");
        }
        if (stepEntity.getReplyContent() == null){
            throw new MedicalException(ResultCodeEnum.MODEL_INVALID_ERROR.getCode(), "提交时回复内容不能为空");
        }
        visitRecordService.confirm(stepEntity);
        return Result.success(null);

    }

}
